package ru.dbms.client.parser;

import java.util.ArrayList;

import java.util.List;

public class Parser {

	private static Parser instance = new Parser();
	
	public Parser() {
		
	}
	
	public static Parser getInstance() {
		return instance;
	}
	
	public Object[][] getRows(String response, int headersCount) throws Exception {
		
		List<List<String>> rows = new ArrayList<List<String>>();
		List<String> data = new ArrayList<String>();
                String line = "";
		int row = 0;
                int pos = 0;
                boolean quoted = false;
		if(response != null) {
                    while(pos < response.length()) {
                        //If data find
                        if (response.charAt(pos) == '[') {
                            pos++;
                            while (quoted || response.charAt(pos) != ']') {
                                char c = response.charAt(pos);
                                if (c == '\\') {
                                    quoted = true;
                                } else {
                                    line += c;
                                    quoted = false;
                                }
                                pos++;
                            }
                            data.add(line);
                            row++;
                            line = "";
                            if (row == headersCount) {
                                List<String> temp = new ArrayList<String>();
                                for (int i = 0; i < data.size(); i++) {
                                    temp.add(data.get(i));
                                }
                                data.clear();
                                rows.add(temp);
                                row = 0;
                            }
                        } else {
                            pos++;
                        }
                    }
                }
		
		Object[][] obj = new Object[rows.size()][];
		for (int i = 0; i < rows.size(); i++)
			obj[i]= rows.get(i).toArray();
		return  obj;
	}
	
        public String getErrorIfExist(String response) {
            String result = "";
            if(response != null) {
                if(response.charAt(0) == '!' && response.charAt(1) == '{') {
                    int pos = 2;
                    while(pos < response.length()) {
                        if(response.charAt(pos) != '}') {
                           result += response.charAt(pos);
                           pos++;
                        } else {
                            return result;
                        }
                    }
                }
            }
            return result;
        }
	public Object[] getColumnsTypes(String response, int len) throws Exception {
            List<String> types = new ArrayList<String>();
            if(response != null) {
                int pos = 1;
                if(response.charAt(0) == '@') {
                String type = "";
                while(response.charAt(pos) != '{') {
                    if(response.charAt(pos) != ',') {
                        type = type + response.charAt(pos);
                        pos++;
                    } else {
                        types.add(type);
                        type = "";
                        pos++;
                    }
                }
                types.add(type);
                  
          
            
        }
            }
              return types.toArray();
        }
	public Object[] getColumnsHeader(String response) throws Exception {
		List<String> headers = new ArrayList<String>();
		if(response != null) {
			int pos = 0;
			String columnName = "";
			while(pos < response.length()) {
				if(response.charAt(pos) == '{') {
					pos++;
					while(response.charAt(pos) != '}') {
						columnName += response.charAt(pos);
						pos++;
					}
					
					headers.add(columnName);
					columnName = "";
				} else {
					pos++;
				}
			}
		} else {
			throw new Exception();
		}
		return headers.toArray();
	}
}
